package com.example.sql_2_code_demo.service.impl;

import com.example.sql_2_code_demo.entity.OrderInvoice;
import com.example.sql_2_code_demo.dao.OrderInvoiceDao;
import com.example.sql_2_code_demo.service.OrderInvoiceService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 订单开票信息；or_order : or_order_invoice => 多对一(OrderInvoice)表服务实现类
 *
 * @author makejava
 * @since 2022-10-09 10:01:36
 */
@Service("orderInvoiceService")
public class OrderInvoiceServiceImpl implements OrderInvoiceService {
    @Resource
    private OrderInvoiceDao orderInvoiceDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OrderInvoice queryById(Long id) {
        return this.orderInvoiceDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param orderInvoice 筛选条件
     * @param pageRequest  分页对象
     * @return 查询结果
     */
    @Override
    public Page<OrderInvoice> queryByPage(OrderInvoice orderInvoice, PageRequest pageRequest) {
        long total = this.orderInvoiceDao.count(orderInvoice);
        return new PageImpl<>(this.orderInvoiceDao.queryAllByLimit(orderInvoice, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param orderInvoice 实例对象
     * @return 实例对象
     */
    @Override
    public OrderInvoice insert(OrderInvoice orderInvoice) {
        this.orderInvoiceDao.insert(orderInvoice);
        return orderInvoice;
    }

    /**
     * 修改数据
     *
     * @param orderInvoice 实例对象
     * @return 实例对象
     */
    @Override
    public OrderInvoice update(OrderInvoice orderInvoice) {
        this.orderInvoiceDao.update(orderInvoice);
        return this.queryById(orderInvoice.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.orderInvoiceDao.deleteById(id) > 0;
    }
}
